﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using ExtAspNet;

namespace HY.WEB.OA
{
    public partial class _goods : System.Web.UI.Page //PageBase
    {
        public string whe = "", stype = "";
        public string us, um, bh;
        public string tab = "review_goods";
        public string appname = "物品领用单";  //很重要，否则找不到审批流程
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            bh = Session["bh"].ToString();
            um = Session["user"].ToString();
            if (!IsPostBack)
            {           
               btnSearch.OnClientClick = Window1.GetShowReference("../function/search.aspx?tab="+tab+"", "高级搜索");

                fill();
                BindGrid();
            }
        }

        private void fill()
        {
            DataTable d = BLL.BLLfun.get_search(tab);
            ddlfiled.Items.Clear();
            for (int i = 0; i < d.Rows.Count; i++)
            {
                ddlfiled.Items.Add(new ExtAspNet.ListItem(d.Rows[i][1].ToString(), d.Rows[i][0].ToString()));
            }
            ddlfiled.DataBind();

            DataTable e = BLL.BLLfun.get_where();
            ddlwhere.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlwhere.Items.Add(new ExtAspNet.ListItem(e.Rows[i][0].ToString(), e.Rows[i][1].ToString()));
            }
             ddlwhere.DataBind();   ddlwhere.SelectedIndex = ddlwhere.Items.IndexOf(ddlwhere.Items.FindByValue("="));




            ////工作人员
            //e = BLL.Bllbase.get_table("[sys_user]");
            //ddlperson.Items.Clear();
            //for (int i = 0; i < e.Rows.Count; i++)
            //{
            //    ddlperson.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["user_name"].ToString().Trim(), e.Rows[i]["sbh"].ToString().Trim()));
            //}
            //ddlperson.DataBind();


            BLL.HR.sys_user su = new BLL.HR.sys_user();
            DataTable  dt=su.GetList("lgid='"+us+"'").Tables[0];

            lblbh.Text = BLL.BllAutoCode.GetMaxBH("review_goods");
            lblname.Text =  dt.Rows[0]["user_name"].ToString();
            
            DateTime dtm = DateTime.Now;         
            date1.Text = dtm.ToString("yyyy-MM-dd");
            date2.Text = dtm.ToString("yyyy-MM-dd");

          //  ---------------
        }

        //姓名
        public  string getnm(string nmid)
        {
            BLL.HR.sys_user u = new BLL.HR.sys_user();
            return BLL.BLLfun.getname(nmid);
         }

        private void BindGrid()
        {

            if (whe == "")
                whe = whe + "  pbh='" + bh + "'";
            else
                whe = whe + " and pbh='" + bh + "'";

            DataTable table2 = BLL.Bllbase.get_table("[review_goods]",whe);           

            DataView view1 = table2.DefaultView;
            view1.Sort = String.Format("{0} {1}","sid","desc");

            Grid2.DataSource = view1;
            Grid2.DataBind();

           // DataTable table3 = BLL.oa.review.get_list_review(" and reviewnm='" + us + "'");
           // Grid3.DataSource = table3;
           // Grid3.DataBind();

        }

      

        protected void Grid2_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid2.PageIndex = e.NewPageIndex;
        }

        protected void Grid2_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            if (whe == "")
                whe = whe + "  pbh='" + bh + "'";
            else
                whe = whe + " and pbh='" + bh + "'";

            DataTable table = BLL.Bllbase.get_table("[review_goods]", whe);   

             DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid2.DataSource = view1;
        }

    

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            txt.ShowTrigger1 = true;
            string ddlf = ddlfiled.SelectedValue;
            string ddlw = ddlwhere.SelectedValue;
           string key = txt.Text.Trim();
            if (ddlw.Equals("="))
                whe = "" + ddlf + " like  '%" + key + "%'";
            else
            {
                if (ddlf == "sid" || ddlf == "编号")
                    whe = "" + ddlf + " " + ddlw + " " + key + "";
                else
                    whe = "" + ddlf + " " + ddlw + " '" + key + "'";

            }
            if (key == "") whe = "";

            BindGrid();
        }

        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            txt.Text = String.Empty;
            txt.ShowTrigger1 = false;
            BindGrid();
        }


        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int val = Grid2.PageIndex * Grid2.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid2.SelectedRowIndexArray)
            {
                sb.Append(Grid2.DataKeys[row+val][0].ToString());
                sb.Append(",");
            }

            //Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));

            BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','), " gbh not in (SELECT  [reviewBH] FROM [leave_sub] where reviewrun='同意')");
            Alert.ShowInTop("仅能删除未通过审批的单据");
            BindGrid();

        }

       //获取审批状戊
        public string getstatus(string nmid)
        {
            return BLL.oa.leave_sub.getstatus(nmid);

        }

        protected void Grid2_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName == "sub") //修改 
            {
                string dd = Grid2.DataKeys[e.RowIndex][1].ToString();
              
                DataTable dt1 =BLL.oa.leave_sub.getlist(dd);

                //Grid1.DataSource=dt1;
               // Grid1.DataBind();

               

                // txttitle.Text = dt1.Rows[0]["retitle"].ToString().Trim();
                // txtord.Text = dt1.Rows[0]["renm"].ToString().Trim();


                //ddluser.SelectedIndex = 
                //  ddluser.Items.IndexOf(ddluser.Items.FindByValue(getnm( dt1.Rows[0]["renm"].ToString().Trim())));
            }
            else if (e.CommandName == "Del")//删除 
            {

            } 

        }

        //protected void Grid3_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        //{
        //    if (e.CommandName == "sub") //修改 
        //    {            
        //       // string DD = Grid3.DataKeys[e.RowIndex][0].ToString();
        //      //  DataTable dt1 =BLL.oa.review.get_list_review(" and reviewBH='" + DD + "'");
               
        //       // Grid2.DataSource = dt1;
        //        //Grid2.DataBind();
        //    }         

        //}

        protected void Window1_Close(object sender, EventArgs e)
        {
            // Alert.ShowInTop("弹出窗口关闭了！");
           string key = txt.Text.Trim();
          //  stype = hidstype.Text;
            try
            {
                if (key == "") whe = BLL.pub.pubwhere.get_where_sql(us, tab);
                BindGrid();

            }
            catch (Exception ex)
            {
              //  Alert.Show("查询条件错误");
            }
            finally
            {
                whe = "";
            }
        }


        protected void btsave_Click(object sender, EventArgs e)
        {
            string user=us;

            Model.oa.goods s = new Model.oa.goods();
            s.gbh = BLL.BllAutoCode.GetMaxBH(tab);
            s.item = txtitem.Text.Trim();
            s.unit = txtunit.Text.Trim();

            s.qty = txtqty.Text.Trim();

            if (!VAN.IsNumber(s.qty))
                s.qty = "0";
          

            if (VAN.IsNumber(txtprice.Text.Trim()))
                s.price = decimal.Parse(txtprice.Text.Trim());
            else
                s.price = 0;


            s.total = decimal.Parse(s.qty) * s.price;

           
            s.pbh = bh;
            s.pname = lblname.Text;

           
            s.edt = DateTime.Parse(date1.Text);
            s.adt = DateTime.Parse(date2.Text);
            s.remark = txtmemo.Text;

            s.rname = "";
            s.rbh = "";

            if (txtitem.Text == "" | date1.Text == "")
            {
                Alert.Show("请输入申请日期和物品名称");
                return;
            }





            BLL.oa.goods u = new BLL.oa.goods();

            if (hidsid.Text != "")
               u.Update(s);             
            else
                u.Add(s);
                
              //写入审批表
            //u.get_process(s.BH,us);
            BLL.BLLfun.get_process(s.gbh, us, appname);

            hidsid.Text = "";
            BindGrid();
            clear_all();
        }

        void clear_all()
        {
            txtitem.Text = "";
            txtunit.Text = "";
            txtqty.Text = "";
            txtprice.Text = "";
            date1.Text = DateTime.Now.ToString("yyyy-MM-dd");
            txtmemo.Text = "";

            date2.Text = DateTime.Now.ToString("yyyy-MM-dd");

        }
    
    }
}
